﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace dpruna.SharePoint
{
    public class ExtendedLookup : SPFieldLookup
    {
        public const string CP_SearchPageURL = "SearchPageURL";

        private static Dictionary<int, Dictionary<String, String>> customPropertiesDictionary = new Dictionary<int, Dictionary<String, String>>();
        private string searchPageURL = String.Empty;

        public static int? ContextId
        {
            get
            {
                if (SPContext.Current != null)
                    return SPContext.Current.GetHashCode();
                else
                    return null;
            }
        }
        public string SearchPageURL
        {
            get 
            {
                if (ContextId.HasValue && customPropertiesDictionary.ContainsKey(ContextId.Value) && customPropertiesDictionary[ContextId.Value].ContainsKey(CP_SearchPageURL))
                    return customPropertiesDictionary[ContextId.Value][CP_SearchPageURL];
                else
                    return searchPageURL;
            }
            set 
            {
                searchPageURL = value;
            }
        }

        #region Construction
        public ExtendedLookup(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
            this.searchPageURL = base.GetCustomProperty(CP_SearchPageURL) + String.Empty;
        }
        public ExtendedLookup(SPFieldCollection fields, string fieldName, string displayName)
            : base(fields, fieldName, displayName)
        {
            this.searchPageURL = base.GetCustomProperty(CP_SearchPageURL) + String.Empty;
        }
        #endregion

        #region Public Methods
        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl ctl = null;
                if (this.CountRelated)
                    ctl = new ComputedField();
                else if (AllowMultipleValues)
                    ctl = new ExtendedLookupFieldControl();
                else
                    ctl = new ExtendedLookupFieldControl();

                ctl.FieldName = base.InternalName;
                return ctl;
            }
        }

        public override void Update()
        {
            UpdateCustomProperties(false);
        }

        public override void OnAdded(SPAddFieldOptions op)
        {
            base.OnAdded(op);

            UpdateCustomProperties(true);
        }

        public void UpdateFieldCustomProperty(string customProperty, string strValue)
        {
            if (!ContextId.HasValue)
                return;

            if (customPropertiesDictionary.ContainsKey(ContextId.Value))
            {
                if (customPropertiesDictionary[ContextId.Value] != null)
                {
                    if (customPropertiesDictionary[ContextId.Value].ContainsKey(customProperty))
                        customPropertiesDictionary[ContextId.Value][customProperty] = strValue;
                    else
                        customPropertiesDictionary[ContextId.Value].Add(customProperty, strValue);
                }
                else
                {
                    customPropertiesDictionary.Add(ContextId.Value, new Dictionary<string, string>());
                    customPropertiesDictionary[ContextId.Value].Add(customProperty, strValue);
                }
            }
            else
            {
                customPropertiesDictionary.Add(ContextId.Value, new Dictionary<string, string>());
                customPropertiesDictionary[ContextId.Value][customProperty] = strValue;
            }
        } 
        #endregion

        #region Private Methods
        private void UpdateCustomProperties(bool isNewField)
        {
            if (ContextId.HasValue && this.TypeAsString == "ExtendedLookup")
                base.SetCustomProperty(CP_SearchPageURL, this.SearchPageURL);

            base.Update();

            if (ContextId.HasValue && this.TypeAsString == "ExtendedLookup")
                customPropertiesDictionary.Remove(ContextId.Value);
        } 
        #endregion
    }
}
